# -*- coding: utf-8 -*-
import cv2 as cv
from mediapipe.python.solutions import drawing_utils, face_mesh

cap = cv.VideoCapture("videos/face2.mp4")

mpFaceMesh = face_mesh.FaceMesh(max_num_faces=2)
mpDrawspec = drawing_utils.DrawingSpec(thickness=1, circle_radius=1)

while True:
    flag, frame = cap.read()
    (height, width) = frame.shape[:2]

    frameRGB = cv.cvtColor(frame, cv.COLOR_BGR2RGB)
    results = mpFaceMesh.process(frameRGB)

    if not results.multi_face_landmarks:
        continue

    for facelms in results.multi_face_landmarks:
        # drawing_utils.draw_landmarks(frame, facelms, face_mesh.FACE_CONNECTIONS)
        drawing_utils.draw_landmarks(frame, facelms, face_mesh.FACE_CONNECTIONS, mpDrawspec, mpDrawspec)

        # 打印face上面点的坐标打印出来
        # lm.x和lm.y在[0.0, 1.0]范围内，通过图像的宽度和高度进行归一化
        # lm.z代表地标深度，以头部中央的深度为原点，值越小，地标越靠近相机
        print(len(facelms.landmark))
        for lm in facelms.landmark:
            x = int(lm.x * width)
            y = int(lm.y * height)
            print(x, y)

    cv.imshow("video", frame)
    if cv.waitKey(0) & 0xFF == ord('q'):
        break

cap.release()
cv.destroyAllWindows()